\subsubsection{Decoding}
Once the signal has been demodulated it should ideally look similar to \cref{fig:manchesterencoding} or \cref{fig:millerencoding} for tag to reader or reader to tag communication respectively. 

Communication from the reader to the tag is filtered out as it's always the same and only the UID sent by the tag is interesting for the purposes of this project. The minimum period between two Miller encoded (reader to tag) bits is $12\ \mu$s (\cite{ISO:14443-2} section 8.1) which is significantly longer than the maximum of two falling edges of Manchester encoding (tag to reader) so it can easily be filtered out.

From the NFC specification (ISO 14443-2) we know a few things about Manchester encoding used for tag to reader encoding: 
\begin{itemize}
\item Data is at $848$ kHz which means a period of $1.179\ \mu$s (\cite{ISO:14443-2} section 8.2)
\item The maximum time between two falling edges in a Manchester encoded sequence is $~2.3\ \mu$s
\item A logical '$1$' is represented by a high to low transition.
\item A logical '$0$' is represented by low to high transition.
\item A data sequence is always started with a high to low transition.
\item The end of a data sequence is indicated by an unmodulated signal.
\end{itemize}

\begin{figure}[h]
\includegraphics[center]{assets/manchesterencoding.png}
\caption{An example of Manchester encoding \cite{Wiki:ManchesterEncodingPng}}
\label{fig:manchesterencoding}
\end{figure}

\begin{figure}[h]
\includegraphics[center]{assets/millerencoding.png}
\caption{An example of Miller encoding}
\label{fig:millerencoding}
\end{figure}

As the start of a data sequence is a high to low transition and the specifications don't specify what happens before this it was chosen to use falling edge detection. This way it can be assumed the first received bit is always a $1$.
Using falling-edge detection the time between two edges can be measured. By inspecting numerous test sequences it was discovered that there are only a few possible patterns for this timing. The Matlab scripts used for this discovery can be found in \cref{sec:matlabdecoding}. 

Using these patterns a simple algorithm can be created that determines the received bits by looking at the previous bit. The patterns and an implementation of this algorithm in Matlab can be found in \cref{sec:matlabdecoding}

One possible problem that may arise when using previous bits to generate the next bits is that the initial received bit is wrongly assumed to be one. This could happen due to start-up time in the demodulation circuit, causing the correct value of a received $1$ to actually be $0$. Due to the fact that the periods are symmetric (a period that causes a transition from $0$ to $1$ also causes a transition from 1 to 0) this wrong assumption would lead to a fully inverted sequence.

The UID being the only data of interest this can be easily solved. The UID transfer has a unique length of 5 bytes, 4 bytes of the UID and a Block Check Character (BCC). Each block is followed by a parity bit which can be used to detect corrupted bytes. A total of $5 * (8 + 1) = 45$ bits should be received. If less than $45$ bits are received, start bits could be missing. Using the parity bits, the integrity if the fully received bytes can be checked. If at least 1 bit of the first byte is received the BCC can be used to check the validity of the UID.

The BCC consists of the logical XOR of the first four bytes. Using the property of XOR that the XOR of a sequence A is the same as the XOR of the inverse of A, the first byte can always be reconstructed to its correct value. This reconstructed first byte can be checked against any bits of the first byte that were received to determine whether the rest of the received bits should be inverted.

After checking the integrity of the received UID bytes using the parity bits and a possible inversion using the BCC, the UID can be passed on to the emulator and used to access printers or buildings.